package net.lzzy.practices.web.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Massages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.UserService;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.VmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/*
 * @author admin
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_USER_ROOT)
@Api(tags = "用户管理")
public class UserApiController {
    private final UserService service;
    @Autowired
    public UserApiController(UserService service) {
        this.service = service;
    }
    @ApiOperation("获取所有用户")
    @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true)
    @GetMapping(ApiConstant.ROUTE_USER_ALL)
    public ApiResult getUsers(@RequestParam String token){
        List<VmUser> users =new ArrayList<>();
        service.get().forEach(u->users.add(VmUser.create(u,true)));
        return new ApiResult(true,users.size()+"",users);
    }
    @ApiOperation("获取分页用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int",dataTypeClass = int.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "size",value = "每页大小",dataType = "int",dataTypeClass = int.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true)
    })
    @GetMapping(ApiConstant.ROUTE_USER_PAGE)
    public ApiResult getPageUsers(@RequestParam int page,@RequestParam int size,@RequestParam String token){
        List<VmUser> users =new ArrayList<>();
        service.get(page,size).forEach(u->users.add(VmUser.create(u,true)));
        return new ApiResult(true,service.count()+"",users);
    }
    @ApiOperation("搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kw",value = "关键字",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true)
    })
    @GetMapping(ApiConstant.ROUTE_USER_SEARCH)
    public ApiResult search(@RequestParam String token, @PathVariable String kw){
        List<VmUser> users =new ArrayList<>();
        service.search(kw).forEach(u->users.add(VmUser.create(u,true)));
        return new ApiResult(true,users.size()+"",users);
    }
    @ApiOperation("获取待审核教师")
    @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true)
    @GetMapping(ApiConstant.ROUTE_USER_APPLYING)
    public ApiResult getApplyingTeacher(@RequestParam String token){
        List<VmUser> users =new ArrayList<>();
        service.getByApplyTeacher(true).forEach(u->users.add(VmUser.create(u,true)));
        return new ApiResult(true,users.size()+"",users);
    }
    @ApiOperation("审核教师申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "path",required = true)
    })
    @PostMapping(ApiConstant.ROUTE_USER_APPROVE)
    public ApiResult approve(@RequestParam String token,@RequestParam UUID id){
        User user = service.approveTeacher(id);
        if (user==null){
            return  new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Massages.SUCCESS.toString(), VmUser.create(user,true));
    }
    @ApiOperation("拒绝教师申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "path",required = true)
    })
    @PostMapping(ApiConstant.ROUTE_USER_DECLINE)
    public ApiResult decline(@RequestParam String token,@RequestParam UUID id){
        User user = service.declineTeacher(id);
        if (user==null){
            return  new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Massages.SUCCESS.toString(), VmUser.create(user,true));
    }
    @ApiOperation("封禁用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "令牌",dataType = "String",dataTypeClass = String.class,paramType = "path",required = true),
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "UUID",dataTypeClass = UUID.class,paramType = "path",required = true)
    })
    @PostMapping(ApiConstant.ROUTE_USER_BAN)
    public ApiResult ban(@RequestParam String token,@RequestParam UUID id){
        User user = service.banUser(id);
        if (user==null){
            return  new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
        return new ApiResult(true, Massages.SUCCESS.toString(), VmUser.create(user,true));
    }

}
